/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.my_figure;

/**
 *
 * @author JSJSYS
 */
public class Figure_Frame extends javax.swing.JFrame {

    /**
     * Creates new form Figure_Frame
     */
    public int m_count = 0, flag = 0;//m_count为输入计数，flag为前后计数
    public String str1 = "", str2 = "";//存储字符串

    public enum mode {
        plus, minus, cheng, chu, mod, AC, xsd
    }//处理模式，加减乘除，正负化，归零等等等
    public mode currmode, lxmode = null;
    public Figure_Frame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        TextPrint = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        ButtonAC = new javax.swing.JButton();
        ButtonZorF = new javax.swing.JButton();
        ButtonYuShu = new javax.swing.JButton();
        Buttonchu = new javax.swing.JButton();
        Button7 = new javax.swing.JButton();
        Button8 = new javax.swing.JButton();
        Button9 = new javax.swing.JButton();
        Buttoncheng = new javax.swing.JButton();
        Button4 = new javax.swing.JButton();
        Button5 = new javax.swing.JButton();
        Button6 = new javax.swing.JButton();
        Buttonminus = new javax.swing.JButton();
        Button1 = new javax.swing.JButton();
        Button2 = new javax.swing.JButton();
        Button3 = new javax.swing.JButton();
        Buttonplus = new javax.swing.JButton();
        Button0 = new javax.swing.JButton();
        Buttonxsd = new javax.swing.JButton();
        ButtonSpecial = new javax.swing.JButton();
        Buttondeng = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("哈哈计算器？？？");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "计算器 alpha 1.0.0", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("楷体", 0, 12))); // NOI18N

        TextPrint.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        TextPrint.setText("0");
        TextPrint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TextPrintActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(TextPrint)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(TextPrint, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setLayout(new java.awt.GridLayout(5, 4));

        ButtonAC.setText("AC");
        ButtonAC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonACActionPerformed(evt);
            }
        });
        jPanel2.add(ButtonAC);

        ButtonZorF.setText("+/-");
        ButtonZorF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonZorFActionPerformed(evt);
            }
        });
        jPanel2.add(ButtonZorF);

        ButtonYuShu.setText("%");
        ButtonYuShu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonYuShuActionPerformed(evt);
            }
        });
        jPanel2.add(ButtonYuShu);

        Buttonchu.setText("÷");
        Buttonchu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonchuActionPerformed(evt);
            }
        });
        jPanel2.add(Buttonchu);

        Button7.setText("7");
        Button7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button7ActionPerformed(evt);
            }
        });
        jPanel2.add(Button7);

        Button8.setText("8");
        Button8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button8ActionPerformed(evt);
            }
        });
        jPanel2.add(Button8);

        Button9.setText("9");
        Button9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button9ActionPerformed(evt);
            }
        });
        jPanel2.add(Button9);

        Buttoncheng.setText("x");
        Buttoncheng.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonchengActionPerformed(evt);
            }
        });
        jPanel2.add(Buttoncheng);

        Button4.setText("4");
        Button4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button4ActionPerformed(evt);
            }
        });
        jPanel2.add(Button4);

        Button5.setText("5");
        Button5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button5ActionPerformed(evt);
            }
        });
        jPanel2.add(Button5);

        Button6.setText("6");
        Button6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button6ActionPerformed(evt);
            }
        });
        jPanel2.add(Button6);

        Buttonminus.setText("-");
        Buttonminus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonminusActionPerformed(evt);
            }
        });
        jPanel2.add(Buttonminus);

        Button1.setText("1");
        Button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button1ActionPerformed(evt);
            }
        });
        jPanel2.add(Button1);

        Button2.setText("2");
        Button2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button2ActionPerformed(evt);
            }
        });
        jPanel2.add(Button2);

        Button3.setText("3");
        Button3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button3ActionPerformed(evt);
            }
        });
        jPanel2.add(Button3);

        Buttonplus.setText("+");
        Buttonplus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonplusActionPerformed(evt);
            }
        });
        jPanel2.add(Buttonplus);

        Button0.setText("0");
        Button0.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Button0ActionPerformed(evt);
            }
        });
        jPanel2.add(Button0);

        Buttonxsd.setText(".");
        Buttonxsd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonxsdActionPerformed(evt);
            }
        });
        jPanel2.add(Buttonxsd);

        ButtonSpecial.setText("特殊事件");
        ButtonSpecial.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonSpecialActionPerformed(evt);
            }
        });
        jPanel2.add(ButtonSpecial);

        Buttondeng.setText("=");
        Buttondeng.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtondengActionPerformed(evt);
            }
        });
        jPanel2.add(Buttondeng);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 273, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void create(String n)
    {
        String print;
        if (flag == 0) {
            this.str1 = this.str1 + n;
            print = str1;
        } else {
            this.str2 = this.str2 + n;
            print = str2;
        }
        this.TextPrint.setText(print);
    }
    private void modedeal()
    {
        int solve = 0;
        double solve2 = 0.0;
        String print = "";
        if (null != currmode) switch (currmode) {
            case plus:
                if (lxmode == null) {
                    solve = Integer.parseInt(str1) + Integer.parseInt(str2);
                } else {
                    solve2 = Double.parseDouble(str1) + Double.parseDouble(str2);
                }   break;
            case cheng:
                if (lxmode == null) {
                    solve = Integer.parseInt(str1) * Integer.parseInt(str2);
                } else {
                    solve2 = Double.parseDouble(str1) * Double.parseDouble(str2);
                }   break;
            case chu:
                if (lxmode == null) {
                    solve = Integer.parseInt(str1) / Integer.parseInt(str2);
                } else {
                    solve2 = Double.parseDouble(str1) / Double.parseDouble(str2);
                }   break;
            case minus:
                if (lxmode == null) {
                    solve = Integer.parseInt(str1) - Integer.parseInt(str2);
                } else {
                    solve2 = Double.parseDouble(str1) - Double.parseDouble(str2);
                }   break;
            case mod:
                if (lxmode == null) {
                    solve = Integer.parseInt(str1) % Integer.parseInt(str2);
                } else {
                    solve2 = Double.parseDouble(str1) % Double.parseDouble(str2);
                }   break;
            default:
                break;
        }
        if (lxmode == null) {
            this.str1=print+solve;
            this.str2="";
            flag=1;
            this.TextPrint.setText(str1);
        } else {
            this.str1=print+solve2;
            this.str2="";
            flag=1;
            this.TextPrint.setText(str1);
        }
    }
    private void Cxoperator()//不运用等号，可以连续运算
    {
        if(m_count>1)
            this.modedeal();
        this.m_count++;
    }
    private void TextPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TextPrintActionPerformed
        // TODO add your handling code here:
//        String str=this.TextPrint.getText();
//        if(this.m_count==1)
//        {    
//            this.Figure_num1=Integer.parseInt(str);
//            this.TextPrint.setText(str);
//        }
//        else
//        {   
//            this.Figure_num2=Integer.parseInt(str);
//            this.TextPrint.setText(str);
//            this.m_count=1;
//        }
    }//GEN-LAST:event_TextPrintActionPerformed

    private void Button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Button1ActionPerformed
        // TODO add your handling code here:
        create("1");

    }//GEN-LAST:event_Button1ActionPerformed

    private void ButtonplusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonplusActionPerformed
        // TODO add your handling code here:
        this.flag++;
        currmode = mode.plus;
        Cxoperator();
    }//GEN-LAST:event_ButtonplusActionPerformed

    private void ButtondengActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtondengActionPerformed
        // TODO add your handling code here:
        this.modedeal();

    }//GEN-LAST:event_ButtondengActionPerformed

    private void Button2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Button2ActionPerformed
        // TODO add your handling code here:
        create("2");
    }//GEN-LAST:event_Button2ActionPerformed

    private void Button3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Button3ActionPerformed
        // TODO add your handling code here:
        create("3");
    }//GEN-LAST:event_Button3ActionPerformed

    private void Button4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Button4ActionPerformed
        // TODO add your handling code here:
        create("4");
    }//GEN-LAST:event_Button4ActionPerformed

    private void Button5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Button5ActionPerformed
        // TODO add your handling code here:
        create("5");
    }//GEN-LAST:event_Button5ActionPerformed

    private void Button6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Button6ActionPerformed
        // TODO add your handling code here:
        create("6");
    }//GEN-LAST:event_Button6ActionPerformed

    private void Button7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Button7ActionPerformed
        // TODO add your handling code here:
        create("7");
    }//GEN-LAST:event_Button7ActionPerformed

    private void Button8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Button8ActionPerformed
        // TODO add your handling code here:
        create("8");
    }//GEN-LAST:event_Button8ActionPerformed

    private void Button9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Button9ActionPerformed
        // TODO add your handling code here:
        create("9");
    }//GEN-LAST:event_Button9ActionPerformed

    private void ButtonminusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonminusActionPerformed
        // TODO add your handling code here:
        this.flag++;
        currmode = mode.minus;
        Cxoperator();
    }//GEN-LAST:event_ButtonminusActionPerformed

    private void ButtonchengActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonchengActionPerformed
        // TODO add your handling code here:
        this.flag++;
        currmode = mode.cheng;
        Cxoperator();

    }//GEN-LAST:event_ButtonchengActionPerformed

    private void ButtonchuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonchuActionPerformed
        // TODO add your handling code here:
        this.flag++;
        currmode = mode.chu;
        Cxoperator();
    }//GEN-LAST:event_ButtonchuActionPerformed

    private void ButtonYuShuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonYuShuActionPerformed
        // TODO add your handling code here:
        this.flag++;
        currmode = mode.mod;
        Cxoperator();
    }//GEN-LAST:event_ButtonYuShuActionPerformed

    private void ButtonZorFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonZorFActionPerformed
        // TODO add your handling code here:
        String print;
        if (flag == 0 && Integer.parseInt(str1) > 0) {
            this.str1 = "-" + this.str1;
            print = str1;
        } else if (flag == 0 && Integer.parseInt(str1) < 0) {
            this.str1 = this.str1.replace("-", "");
            print = str1;
        } else if (flag == 1 && Integer.parseInt(str2) > 0) {
            this.str2 = "-" + this.str2;
            print = str2;
        } else {
            this.str2 = this.str2.replace("-", "");
            print = str2;
        }
        this.TextPrint.setText(print);
    }//GEN-LAST:event_ButtonZorFActionPerformed

    private void ButtonACActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonACActionPerformed
        // TODO add your handling code here:
        str1 = "";
        str2 = "";
        flag=0;
        this.TextPrint.setText("0");
    }//GEN-LAST:event_ButtonACActionPerformed

    private void ButtonxsdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonxsdActionPerformed
        // TODO add your handling code here:
       create(".");
       this.lxmode=mode.xsd;
       
    }//GEN-LAST:event_ButtonxsdActionPerformed

    private void ButtonSpecialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonSpecialActionPerformed
        // TODO add your handling code here:
        new SpecialFrame().setVisible(true);
    }//GEN-LAST:event_ButtonSpecialActionPerformed

    private void Button0ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Button0ActionPerformed
        // TODO add your handling code here:
        create("0");
    }//GEN-LAST:event_Button0ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Figure_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Figure_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Figure_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Figure_Frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Figure_Frame().setVisible(true);          
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Button0;
    private javax.swing.JButton Button1;
    private javax.swing.JButton Button2;
    private javax.swing.JButton Button3;
    private javax.swing.JButton Button4;
    private javax.swing.JButton Button5;
    private javax.swing.JButton Button6;
    private javax.swing.JButton Button7;
    private javax.swing.JButton Button8;
    private javax.swing.JButton Button9;
    private javax.swing.JButton ButtonAC;
    private javax.swing.JButton ButtonSpecial;
    private javax.swing.JButton ButtonYuShu;
    private javax.swing.JButton ButtonZorF;
    private javax.swing.JButton Buttoncheng;
    private javax.swing.JButton Buttonchu;
    private javax.swing.JButton Buttondeng;
    private javax.swing.JButton Buttonminus;
    private javax.swing.JButton Buttonplus;
    private javax.swing.JButton Buttonxsd;
    private javax.swing.JTextField TextPrint;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables
}
